
const HomeService = require('../service/home');
const {
    getCategoryList,
    getCategoryListId,
} = require('../model')
const { formatTime } = require('../lib/utils');

module.exports = {
    index : async (ctx,next)=>{

        ctx.response.type = 'text/html'
        console.log(ctx.request.method)
        if(ctx.request.method === 'GET') {
            console.log(ctx.request.path, ctx.request.query.id, ctx.request.params)
        }
        // ctx.response.status = 200
        // ctx.throw(500) //Internal server error 抛出错误
        ctx.response.body = '<h2>hello fire-eye!</h2>'
    },
    user : async (ctx,next)=>{
        ctx.status = 200;

        await ctx.render( 'user/register', {
            btnTitle : '注册提交',
            categoryList : await getCategoryList()
        } )
    },
    userRegister : async(ctx, next)=>{
        let {name, password} = ctx.request.body
        ctx.response.body = await HomeService.register(name, password)

    },
    userDetail :  async (ctx,next)=>{
        console.log(ctx.params)
        let { id } = ctx.params
        let _content = await getCategoryListId(id)
         await ctx.render('user/detail', {
            content : _content,
             getTime : formatTime(_content.createdAt)
        })
    },
    error : async (ctx,next)=>{
        ctx.response.body = '<h1>404 Not Found</h1>'
    },
    userJson: async(ctx,next) =>{
        ctx.body = [
            {
                name : 'leonchua',
                age : 30
            },
            {
                name : 'fire-eye',
                age : 29
            }
        ]
    }
}